package com.example.lotteryticketdev.tool;

import com.example.lotteryticketdev.entity.Ball;

import java.lang.reflect.Field;
import java.util.Date;

public class MyReflect {

    public static Object setForNotNull(Object o1 , Object o2){
        Class<?> clazz1 = o1.getClass();
        Class<?> clazz2 = o2.getClass();
        Field[] fields = clazz1.getDeclaredFields();
        for (Field field : fields) {
             //强制访问
            field.setAccessible(true);
            //获取属性名字
            String name = field.getName();

            try {
                //获取属性值
                Object value = field.get(o1);
                if(value != null){
                    Field temp = clazz2.getDeclaredField(name);
                    //强制访问
                    temp.setAccessible(true);
                    temp.set(o2,value);
                }
                

            } catch (IllegalAccessException | NoSuchFieldException e) {
                e.printStackTrace();
            }


        }

        return o2;

    }

    public static void main(String[] args) {
        Ball ball1 = new Ball();
        ball1.setRed("11,22,33");
        ball1.setBlue("111");
        Ball ball2 = new Ball();
        ball2.setBlue("22222");
        ball2.setCreateTime(new Date());
        Ball o = (Ball)MyReflect.setForNotNull(ball1, ball2);
        System.out.println("o = " + o.getCreateTime());
    }
}
